package com.example.websocket.config.core.json;

import com.example.websocket.config.core.SpringUtils;
import com.example.websocket.config.core.json.annotation.ExtendField;
import com.example.websocket.config.core.json.extender.FieldExtender;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.List;

@Component
public class ExtendFieldSerializerModifier extends BeanSerializerModifier {

    @Override
    public List<BeanPropertyWriter> changeProperties(
            SerializationConfig config,
            BeanDescription beanDesc,
            List<BeanPropertyWriter> beanProperties) {
        for (BeanPropertyWriter writer : beanProperties) {
            ExtendField annotation = writer.getAnnotation(ExtendField.class);
            if (annotation != null) {
                // 为带有ExtendField注解的字段创建自定义序列化器
                String propertieName = writer.getName();
                writer.assignSerializer(new ExtendFieldSerializer(writer.getSerializer(), annotation, propertieName));
            }
        }
        return beanProperties;
    }

    private static class ExtendFieldSerializer extends JsonSerializer<Object> {
        private final JsonSerializer<Object> defaultSerializer;
        private final ExtendField annotation;
        private final String propertieName;
        private final FieldExtender extender;

        public ExtendFieldSerializer(JsonSerializer<Object> defaultSerializer, ExtendField annotation, String propertieName) {
            this.defaultSerializer = defaultSerializer;
            this.annotation = annotation;
            this.propertieName = propertieName;
            extender = SpringUtils.getBean(annotation.extender());
        }

        @Override
        public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {
            String extendedName = annotation.name();
            // 先使用默认序列化器序列化原始字段
            if (defaultSerializer != null) {
                defaultSerializer.serialize(value, gen, provider);
            } else {
                // Fallback for null serializer
                provider.defaultSerializeValue(value, gen);
            }

            try {
                Object extendedValue = null;
                // Only process if value is not null
                if (value != null) {
                    // 实例化字段扩展器
                    // FieldExtender extender = annotation.extender().newInstance();
                    // 处理扩展逻辑
                    extendedValue = extender.extend(value);
                }
                // 写入扩展字段
                // if (extendedValue != null) {
                extendedName = StringUtils.hasText(extendedName) ? extendedName : propertieName + "Text";
                gen.writeObjectField(extendedName, extendedValue);
                // }
            } catch (Exception e) {
                throw new IOException("Failed to process extended field: " + extendedName, e);
            }
        }
    }
}
